import request from '@/utils/request'

// 销售机会列表响应接口
export interface OpportunityListResponse {
  list: Opportunity[]
  total: number
  page: number
  limit: number
}

// 销售机会类型定义
export interface Opportunity {
  id: number
  customer_id: number
  primary_contact_id: number
  service_type_id: number
  title: string
  category: number
  level: number
  source: string
  status: number
  convert_order_id: number
  convert_time: number
  lose_reason: string
  lose_time: number
  user_name: string
  department_id: number
  remarks: string
  discovery_date: number
  expected_sign_date: number
  expected_amount: number
  user_id: number
  create_time: number
  update_time: number
  // 关联字段
  customer_name?: string
  contact_name?: string
  service_type_name?: string
  user_name_display?: string
}

// 销售机会搜索参数
export interface OpportunitySearchParams {
  page?: number
  limit?: number
  customer_id?: number
  customerName?: string
  category?: number
  level?: number
  status?: number
  user_id?: number
  department_id?: number
  start_date?: number
  end_date?: number
}

// 销售机会API
export const opportunityApi = {
  // 获取销售机会列表
  getOpportunityList: (params: OpportunitySearchParams) => {
    return request({
      url: '/api/crm/opportunity/list',
      method: 'get',
      params,
    })
  },

  // 获取销售机会详情
  getOpportunityDetail: (params: { id: number }) => {
    return request({
      url: '/api/crm/opportunity/detail',
      method: 'get',
      params,
    })
  },

  // 新增销售机会
  addOpportunity: (data: Partial<Opportunity>) => {
    return request({
      url: '/api/crm/opportunity/add',
      method: 'post',
      data,
    })
  },

  // 更新销售机会
  updateOpportunity: (data: Partial<Opportunity> & { id: number }) => {
    return request({
      url: '/api/crm/opportunity/update',
      method: 'put',
      data,
    })
  },

  // 删除销售机会
  deleteOpportunity: (data: { id: number }) => {
    return request({
      url: '/api/crm/opportunity/delete',
      method: 'post',
      data,
    })
  },

  // 转化销售机会为订单
  convertOpportunity: (data: { id: number }) => {
    return request({
      url: '/api/crm/opportunity/convert',
      method: 'post',
      data,
    })
  },

  // 标记销售机会失效
  markLoseOpportunity: (data: { id: number; reason: string }) => {
    return request({
      url: '/api/crm/opportunity/markLose',
      method: 'post',
      data,
    })
  },

  // 获取销售机会统计
  getOpportunityStatistics: (params?: { user_id?: number; department_id?: number }) => {
    return request({
      url: '/api/crm/opportunity/statistics',
      method: 'get',
      params,
    })
  },

  // 获取销售机会阶段选项
  getStageOptions: () => {
    return request({
      url: '/api/crm/opportunity/stageOptions',
      method: 'get',
    })
  },

  // 获取销售机会等级选项
  getLevelOptions: () => {
    return request({
      url: '/api/crm/opportunity/levelOptions',
      method: 'get',
    })
  },

  // 获取销售机会类别选项
  getCategoryOptions: () => {
    return request({
      url: '/api/crm/opportunity/categoryOptions',
      method: 'get',
    })
  },

  // 获取销售机会状态选项
  getStatusOptions: () => {
    return request({
      url: '/api/crm/opportunity/statusOptions',
      method: 'get',
    })
  },

  // 批量操作销售机会
  batchOperation: (data: {
    ids: number[]
    action: 'assign_user' | 'delete' | 'mark_lose'
    user_id?: number
    reason?: string
  }) => {
    return request({
      url: '/api/crm/opportunity/batch',
      method: 'post',
      data,
    })
  },

  // 获取销售机会的跟进记录
  getOpportunityTrace: (params: { opportunity_id: number; page?: number; limit?: number }) => {
    return request({
      url: '/api/crm/opportunity/trace',
      method: 'get',
      params,
    })
  },

  // 添加销售机会跟进记录
  addOpportunityTrace: (data: {
    opportunity_id: number
    content: string
    next_followup_time: number
  }) => {
    return request({
      url: '/api/crm/opportunity/addTrace',
      method: 'post',
      data,
    })
  },
}

export default opportunityApi
